home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / basic / tutorqb.com / TUTOR-03.BAS < prev    next >
Encoding:
BASIC Source File  |  1986-12-04  |  10.9 KB  |  248 lines

  1. goto example1
  2. '                       The QuickBASIC Tutorial Series
  3.  
  4. '                                   Part 3
  5. '                      Keyboard Input & Screen Handling
  6.  
  7. '                        Copyright 1986  Ford Software
  8.  
  9. '                            4845 Willowbend Blvd.
  10. '                              Houston, TX 77035
  11. '                                (713) 721-5205
  12. '                          CompuServe ppn: 71355,470
  13.  
  14. 'This Tutorial may not be copied or distributed in any form - printed, on disk
  15. 'or electronically - without the express written permission of Ford Software.
  16. '           Unlicensed distribution is a violation of copyright law
  17. '            and may be subject to civil and criminal prosecution.
  18.  
  19. 'QuickBASIC is the registered trademark of the Microsoft Corporation.
  20. 'Ford Software is not associated in any way with the Microsoft Corporation.
  21.  
  22. '(Make sure NumLock is off and press PgDn)
  23. 'page 2
  24. '                           I N T R O D U C T I O N
  25.  
  26. 'This lesson will cover keyboard input and working with the screen. It will
  27. 'not cover graphics because that is a specialized and more advanced topic.
  28.  
  29. 'To run examples of code in this and other lessons, first press PgUp to get to
  30. 'the opening screen. The first line of the screen will say "goto example#".
  31. 'Change it to the example number you wish to run. Press Ctrl-R to compile and
  32. 'run the code. When done, press Ctrl-F and enter "page " plus the page number
  33. 'you were on. You will have to use Ctrl-Up and -Down to realign the page on
  34. 'the screen correctly. Study the code and press Ctrl-R to see it run again.
  35. 'This is the best way to learn. If you don't undertand something, turn to that
  36. 'part of your QuickBASIC manual for more information.
  37.  
  38. 'You should also feel free to try typing in code of your own anytime you wish
  39. 'to aid in your understanding. Insert your code before the END statement of
  40. 'the example you are running. You can also delete or modify the example if you
  41. 'wish. You can always reload this file from the disk if you mess up what is in
  42. 'memory.
  43. '
  44. '(Press PgDn)
  45. 'page 3
  46. '                                 Contents
  47.  
  48. '                page
  49. '                  4         INPUT Command
  50. '                  5         LINE INPUT Command
  51. '                  6         INKEY$ Function
  52. '                  7         Printing to the Screen
  53. '                  8         PRINT USING Command
  54. '                  9         LOCATE Command
  55. '                 10         Turning Cursor Off/On
  56. '                 10         Changing Size of Cursor
  57. '                 11         CLS Command
  58. '                 11         CSRLIN Function
  59. '                 11         POS(0) Function
  60. '                 11         COLOR Command
  61.  
  62.  
  63.  
  64.  
  65. '                To search for specific topics, use Ctrl-F.
  66. '(Press PgDn)
  67. 'page 4                          INPUT Command
  68.  
  69. 'INPUT is the quickest and easiest way to get input from the keyboard, but
  70. 'allows the programmer very little control over how data is entered or what
  71. 'type of data is entered.
  72. example1:
  73. INPUT "Enter first name"; NA1$     'This will print "Enter first name?_"
  74. INPUT "Enter last name:", NA2$     'Use a comma after the prompt for no "?".
  75. INPUT "Enter Month, Day, Yr:", MO, DY, YR  'Multi-variable input is the major
  76. 'weakness of the INPUT command. If the user doesn't respond with the correct
  77. 'number of fields seperated by the right number of commas, he gets the not
  78. 'very helpful error message "?Redo from start" and the input field drops down
  79. 'to the next line, messing up any fancy screen layouts you might have had.
  80. 'This one weakness keeps most good programmers from ever using INPUT.
  81. 'Another problem is if the user does not enter the right type of data. You get
  82. 'another sloppy, cryptic error message if you try to enter text when the
  83. 'program wants numbers, as above. The programmer can help by describing on
  84. 'the screen exactly what he wants, but that is not very efficient:
  85. INPUT "Enter Month, Day, Yr in the format 10, 15, 1986: ", MO, DY, YR
  86. 'Also, seperating data by commas is not how people usually write.
  87. END
  88. '   (PgDn)
  89. 'page 5                       (INPUT, continued)
  90.  
  91. 'You can get around that problem in a round-about way:
  92. example2:
  93. LOCATE 10,11 : PRINT "MM/DD/YY";  ' This displays the format to be used.
  94. LOCATE 10,11 : INPUT "", X$       ' The comma suppresses the question mark.
  95. MO$=LEFT$(X$,2)     'Now you can pull the month, day and year out. But life
  96. DY$=MID$(X$,4,2)    'isn't really this easy. You will have to test for X$
  97. YR$=RIGHT$(X$,2)    'being the right length, the "/" being in the right place,
  98.             'and so forth - not a very efficient procedure.
  99. END
  100.  
  101. 'LINE INPUT
  102. 'Since commas are treated by INPUT as seperating data for different variables,
  103. 'you need a way of entering data that will not be bothered by commas and other
  104. 'things that upset the INPUT command. LINE INPUT is the command to do it.
  105. 'Also, LINE INPUT does not add a question mark to your prompt as INPUT does.
  106. example3:
  107. INPUT "Try to enter text with some commas (just press Enter to give up): ", X$
  108. LINE INPUT "Now try again: "; X$
  109. END
  110. '   (PgDn)
  111. 'page 6                             INKEY$
  112.  
  113. 'INKEY$ gets a keypress from the keyboard buffer, if any is there. If you have
  114. 'not already pressed a key, INKEY$ does not wait for you to do so like INPUT
  115. 'does, so when using INKEY$ to get input, you must use a loop:
  116.  
  117. example4:  IN$=""
  118. PRINT "Enter text:"  'Don't worry about this next line now. It just says to
  119. WHILE IN$<>CHR$(13)  '  repeat the loop until the Enter key is pressed.
  120.   IN$=""             'You must make sure the variable is clear before starting
  121.   WHILE IN$=""       '  the loop or the loop may not execute.
  122.     IN$=INKEY$       'You must assign to a variable the keypress returned by
  123.   WEND               '  the INKEY$ function.
  124.   PRINT IN$;         'INKEY$ doesn't echo to the screen. You must explicitly
  125. WEND                 '  print the character. Notice the semicolon after the
  126. PRINT "Done"         '  variable being printed. That makes each letter follow
  127. END                  '  the last instead of being on a line by itself.
  128.  
  129. 'The INKEY$ function is the basis for a sophisticated data entry routine that
  130. 'will be examined in a later lesson.
  131.  
  132. '   (PgDn)
  133. 'page 7                     Printing to the Screen
  134.  
  135. 'PRINT...
  136. '  is the simplest method to print to the screen.
  137. example5:
  138. PRINT "123456789-123456789-"   '(Read about TAB, SPC and SPACE in the manual.)
  139. PRINT TAB(20) "XXXXX"    'TAB(20) skips to the 20th character position
  140. PRINT SPC(20) "yyyyy"    'skips 20 spaces and starts in the 21st position
  141. PRINT SPACE$(20) "ZZ"    'also skips 20 spaces
  142.  
  143. PRINT "semi";     'A semicolon means that the next print command will start at
  144. PRINT "colon"     'the very next character position instead of the next line.
  145. PRINT "se"; "mi"
  146. PRINT "col" "on"   'The semicolon is optional within a PRINT statement.
  147.  
  148. PRINT "comma",    'A comma between fields will cause the next print command to
  149. PRINT "tab"       'skip to the next tab position.
  150. PRINT "tab 1", 2, 3, 4, 5, 6, 7, 8      'This shows the tab positions
  151. END
  152. 'Press Ctrl-R to compile and run this example, then compare to the code above.
  153.  
  154. '   (PgDn)
  155. 'page 8                         PRINT USING...
  156.  
  157. '  lets you format the output to the screen. Although there are a whole lot of
  158. 'fancy formatting options listed under PRINT USING in the manual, 99% of the
  159. 'time, you will be using PRINT USING to format numeric output. The procedure
  160. 'is really very simple.
  161. example6:                              '  EXPLANATIONS:
  162. X=10/3                                 ' Set X equal to 3.333333
  163. PRINT X                                ' This is the unformatted output.
  164. PRINT USING "#####"; X                 ' This truncates the decimal.
  165. PRINT USING "###.#"; X; 2              ' Prints X and 2 to one decimal place.
  166. PRINT USING "##.##"; X; 3              ' Prints X and 3 to two places.
  167. PRINT USING "#.###"; X; 4              ' This will look like "3.3334.000",
  168. PRINT USING "#.###  "; X; 4            '  so we put some space in between.
  169. PRINT USING "$##.## "; X; 5; X*2       ' Most text imbedded in a PRINT USING
  170. PRINT USING "#.## Dollars  "; X; X+X   '  string will print as is.
  171. X$="##.## with variable"               ' The PRINT USING string can be
  172. PRINT USING X$; X                      '  assigned to a variable.
  173. END
  174. 'To run this, press Ctrl-PgUp, change to "goto example6", press Ctrl-R.
  175.  
  176. '   (PgDn)
  177. 'page 9                            LOCATE
  178.  
  179. 'LOCATE is used to make printing begin at a specific point on the screen. The
  180. 'format is "LOCATE x,y", where "x" is the row number (1-25) and y is the
  181. 'column (or horizontal character position) number (1-80).
  182. '(As usual, press Ctrl-PgUp, change to "goto example7", press Ctrl-R to run.)
  183.  
  184. example7:
  185. LOCATE 24,38:PRINT"1: 24,38"   'Printing on lines 24 or 25 without a trailing
  186. LOCATE 24,38:PRINT"2: 24,38;"; 'semicolon makes the screen scroll.
  187. LOCATE 1,15 :PRINT"3: 1,15";  'Print starts at row 1, column 15.
  188. LOCATE 2    :PRINT"4: 2";     'Leaving off the column number starts at the
  189.                   'normal column number on the specified line, in
  190.                   'this case, the end of the last text printed.
  191. LOCATE ,60  :PRINT"5: ,60"    'Leaving off the row number starts print at
  192.                   'the normal row, in this case, row 2, since the
  193.                   'last PRINT statement ended with a semicolon.
  194. LOCATE ,50  :PRINT"6: ,50"    'This will print on row 4, since the last
  195.                   'PRINT statement did not end with a semicolon.
  196. END
  197.  
  198. '   (PgDn)
  199. 'PAGE 10                      (LOCATE, continued)
  200.  
  201. 'Other LOCATE Functions:
  202.  
  203. 'LOCATE 10,30,0  positions the cursor at row 10, column 30, and turns the
  204. 'blinking cursor off. If you just want to toggle the cursor on and off, leave
  205. 'out the row and column numbers:
  206. example8:
  207. LOCATE ,,0 : PRINT "The cursor is off. Press ENTER";
  208. X$="":WHILE X$="":X$=INKEY$:WEND
  209. LOCATE 2,1,1 : PRINT "The cursor is on. Press ENTER";
  210. X$="":WHILE X$="":X$=INKEY$:WEND
  211. LOCATE 3,1
  212. 'Cursor size can be changed with the fourth and fifth positions of LOCATE:
  213. WHILE TOP<>9
  214.   INPUT "Top scan line # for cursor (0-7, 9 TO END)"; TOP
  215.   IF TOP=9 THEN END
  216.   INPUT "Bottom scan line # for cursor (0-7)"; BOT
  217.   LOCATE ,,,TOP,BOT : PRINT
  218. WEND    'The most common reason to change the cursor size is to indicate if
  219.     'the user is in the Insert or Overtype mode.
  220. '   (PgDn)
  221. 'page 11                     Other Video Commands
  222.  
  223. 'CLS, of course, clears the screen.
  224.  
  225. 'CSRLIN tells you what line the cursor is on.  Eg:  ROW=CSRLIN
  226.  
  227. 'POS(0) tells you the horizontal position of the cursor.  Eg: COL=POS(0)
  228.  
  229. 'COLOR x,y,z changes screen colors. The following code will let you try
  230. '      different colors. (Border won't work on EGA, so we left it out.)
  231.  
  232.   example9:
  233.   WHILE FG <> 99
  234.     INPUT "Foreground (0-31, 99 to Quit)"; FG
  235.     IF FG=99 THEN END
  236.     INPUT "Background (0-7, not same as foreground)"; BG
  237.     IF FG<>BG THEN COLOR FG,BG
  238.   WEND
  239.  
  240.  
  241.  
  242. '                               end of lesson 3
  243.  
  244.  
  245.  
  246.  
  247. 'end of file - press PgUp
  248.